/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:24 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class DekiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Dekiemtra Này";
		private static string EX_STENDEKIEMTRA_EMPTY="sTendekiemtra không được để trống";
		private static string EX_DNGAYTAO_INVALID="dNgaytao không hợp lệ";
		private static string EX_DNGAYCAPNHAT_INVALID="dNgaycapnhat không hợp lệ";
		private static string EX_FK_INGUOITAOID_INVALID="FK_iNguoitaoID không hợp lệ";
		private static string EX_FK_INGUOICAPNHATID_INVALID="FK_iNguoicapnhatID không hợp lệ";
		private static string EX_FK_IHINHTHUCKIEMTRAID_INVALID="FK_iHinhthuckiemtraID không hợp lệ";
		private static string EX_ID_INVALID="PK_iDekiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Dekiemtra Theo ID
        /// </summary>
        /// <param name="PK_iDekiemtraID">Int64:Dekiemtra ID</param>
        /// <returns>DekiemtraEntity</returns>        
        public static DekiemtraEntity GetOne(Int64 PK_iDekiemtraID)
        {
            
			if(PK_iDekiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return DekiemtraDAL.GetOne(PK_iDekiemtraID);
        }
        /// <summary>
        /// Lấy về List các Dekiemtra
        /// </summary>
        /// <returns>List DekiemtraEntity:List Dekiemtra Cần lấy</returns>
        public static List<DekiemtraEntity> GetAll()
        {
            return DekiemtraDAL.GetAll();
        }
        public static List<DekiemtraEntity> GetByFK_iNguoitaoID(Int64 FK_iNguoitaoID)
		{
			if(FK_iNguoitaoID<=0)
				throw new Exception(EX_FK_INGUOITAOID_INVALID);
			return DekiemtraDAL.GetByFK_iNguoitaoID(FK_iNguoitaoID);
		}public static List<DekiemtraEntity> GetByFK_iNguoicapnhatID(Int64 FK_iNguoicapnhatID)
		{
			if(FK_iNguoicapnhatID<=0)
				throw new Exception(EX_FK_INGUOICAPNHATID_INVALID);
			return DekiemtraDAL.GetByFK_iNguoicapnhatID(FK_iNguoicapnhatID);
		}public static List<DekiemtraEntity> GetByFK_iHinhthuckiemtraID(Int16 FK_iHinhthuckiemtraID)
		{
			if(FK_iHinhthuckiemtraID<=0)
				throw new Exception(EX_FK_IHINHTHUCKIEMTRAID_INVALID);
			return DekiemtraDAL.GetByFK_iHinhthuckiemtraID(FK_iHinhthuckiemtraID);
		}
        /// <summary>
        /// Kiểm tra và thêm mới Dekiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Dekiemtra Mới Thêm Vào</returns>
        public static Int32 Add(DekiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return DekiemtraDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Dekiemtra
        /// </summary>
        /// <param name="entity">DekiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(DekiemtraEntity entity)
        {
            checkExist(entity.PK_iDekiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return DekiemtraDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Dekiemtra
        /// </summary>
        /// <param name="PK_iDekiemtraID">Int64 : PK_iDekiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iDekiemtraID)
        {
            checkExist(PK_iDekiemtraID);
            return DekiemtraDAL.Remove(PK_iDekiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iDekiemtraID)
        {
            DekiemtraEntity oDekiemtra=DekiemtraDAL.GetOne(PK_iDekiemtraID);
            if(oDekiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">DekiemtraEntity: entity</param>
        private static void checkLogic(DekiemtraEntity entity)
        {   
			if (String.IsNullOrEmpty(entity.sTendekiemtra))
				throw new Exception(EX_STENDEKIEMTRA_EMPTY);
			if (DateTime.Parse("1753-01-01")>entity.dNgaytao)
				throw new Exception(EX_DNGAYTAO_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dNgaycapnhat)
				throw new Exception(EX_DNGAYCAPNHAT_INVALID);
			if (entity.FK_iNguoitaoID < 0)
				throw new Exception(EX_FK_INGUOITAOID_INVALID);
			if (entity.FK_iNguoicapnhatID < 0)
				throw new Exception(EX_FK_INGUOICAPNHATID_INVALID);
			if (entity.FK_iHinhthuckiemtraID < 0)
				throw new Exception(EX_FK_IHINHTHUCKIEMTRAID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">DekiemtraEntity: DekiemtraEntity</param>
        private static void checkDuplicate(DekiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<DekiemtraEntity> list = DekiemtraDAL.GetAll();
            if (list.Exists(
                delegate(DekiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iDekiemtraID != entity.PK_iDekiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">DekiemtraEntity:entity</param>
        private static void checkFK(DekiemtraEntity entity)
        {            
			NguoidungEntity oNguoidung = NguoidungDAL.GetOne(entity.FK_iNguoitaoID);
			if (oNguoidung==null)
			{
				throw new Exception("Không tìm thấy :FK_iNguoitaoID");
			}
			oNguoidung = NguoidungDAL.GetOne(entity.FK_iNguoicapnhatID);
			if (oNguoidung==null)
			{
				throw new Exception("Không tìm thấy :FK_iNguoicapnhatID");
			}
			HinhthuckiemtraEntity oHinhthuckiemtra = HinhthuckiemtraDAL.GetOne(entity.FK_iHinhthuckiemtraID);
			if (oHinhthuckiemtra==null)
			{
				throw new Exception("Không tìm thấy :FK_iHinhthuckiemtraID");
			}
        }
        #endregion
    }
}
